package com.mc2.docapp.disabilityassessmenttool;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.InputFilter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import com.mc2.docapp.disabilityassessmenttool.HandComponent.C_upLimb_add_wgt;
import com.mc2.docapp.disabilityassessmenttool.HandComponent.HC_CustDataType;
import com.mc2.docapp.disabilityassessmenttool.Misc.InputFilterMinMax;

import java.util.HashSet;

import io.realm.Realm;
import io.realm.RealmConfiguration;

import static com.mc2.docapp.disabilityassessmenttool.utils.Statics.getEditTexts;
import static com.mc2.docapp.disabilityassessmenttool.utils.Statics.router;


public class UpLimbAdditionalWgtage extends Activity {

    private static final int id = Activities.UPPPER_LIMB_ADDITIONAL_WEIGHTAGE;

    EditText uppLibAdditionalWgtageLEFT;
    EditText uppLibAdditionalWgtageRIGHT;

    C_upLimb_add_wgt c_upLimb_add_wgt = new C_upLimb_add_wgt();

    //db instance
    private Realm realm;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_up_limb_additional_wgtage);
        uppLibAdditionalWgtageRIGHT = (EditText) findViewById(R.id.uppLibAdditionalWgtageRIGHT);
        uppLibAdditionalWgtageLEFT = (EditText) findViewById(R.id.uppLibAdditionalWgtageLEFT);

        initRealmDB();
        sanitizeInput();
        try {
            //mapUI((realm.where(C_upLimb_add_wgt.class)/*.equalTo("id", id)*/.findAll()).get(0));
            System.out.println();
        } catch (Exception e) {
            System.out.println(e.getCause() + " " + e.getMessage());
        }

        Button saveNnextButton = (Button) findViewById(R.id.saveAndNext);
        saveNnextButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                c_upLimb_add_wgt.setAdditionalWgtage(new HC_CustDataType(Integer.parseInt(IfNull(uppLibAdditionalWgtageRIGHT.getText().toString(), "0")),
                        Integer.parseInt(IfNull(uppLibAdditionalWgtageLEFT.getText().toString(), "0")), 0d, 0, 0));


                try {
//                    System.out.println(new ObjectMapper().writeValueAsString(c_active_rom));
                } catch (Exception e) {
                }
                System.out.println();
                c_upLimb_add_wgt.setId(id);
                NewPatient.finalDto.getClass_upperLimb_component().setClass_additional_weightage(c_upLimb_add_wgt);

                calculateTotalULComp(true);
                calculateTotalULComp(false);

                realm.beginTransaction();
                realm.deleteAll();
                realm.insert(c_upLimb_add_wgt);
                realm.commitTransaction();


                //UpLimbAcRom.finalDto
                Intent intent = new Intent(UpLimbAdditionalWgtage.this, router(UpLimbAdditionalWgtage.class));
                startActivity(intent);
            }
        });
        Button clearAll = (Button) findViewById(R.id.clear);
        clearAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                HashSet<EditText> views = new HashSet<>();
                getEditTexts((ViewGroup) findViewById(R.id.activity_main), views);
                for (EditText h : views)
                    h.setText("");
            }
        });
    }

    private void calculateTotalULComp(Boolean isLeft) {
        if(isLeft) {
            double totArmCom = NewPatient.finalDto.getClass_upperLimb_component().getClass_additional_weightage().getAdditionalWgtage().getActual_left();
            double totHandCOmp = NewPatient.finalDto.getClass_upperLimb_component().getArmPlushandCompLeft();


            double total = totArmCom + totHandCOmp;
            if(total > 100d) {
                total = 100d;
            }
            NewPatient.finalDto.getClass_upperLimb_component().setTotalUPLIMBCompLEFT(total);
        } else {
            double totArmCom = NewPatient.finalDto.getClass_upperLimb_component().getClass_additional_weightage().getAdditionalWgtage().getActual_right();
            double totHandCOmp = NewPatient.finalDto.getClass_upperLimb_component().getArmPlushandCompRight();


            double total = totArmCom + totHandCOmp;
            if(total > 100d) {
                total = 100d;
            }
            NewPatient.finalDto.getClass_upperLimb_component().setTotalUPLIMBCompRight(total);

        }
    }

    private double calByFormula(double a, double b) {
        if(a<b){
            a = a + b;
            b = a - b;
            a = a - b;
        }
        double c = a + (((90 - a) * b) / 90.0);
        return c;
    }

    private void initRealmDB() {
        Realm.init(getApplicationContext());
        RealmConfiguration config = new RealmConfiguration.Builder()
                .name(id + ".realm")
                .build();
        realm = Realm.getInstance(config);
    }

    private void mapUI(C_upLimb_add_wgt oldData) {
        uppLibAdditionalWgtageLEFT.setText(String.valueOf((long) (oldData.getAdditionalWgtage().getActual_left())));
        uppLibAdditionalWgtageRIGHT.setText(String.valueOf((long) (oldData.getAdditionalWgtage()).getActual_right()));

    }

    public String IfNull(String value, String defaultValue) {
        if (value == null || value.isEmpty()) {
            return defaultValue;
        } else {
            return value;
        }
    }


    public void sanitizeInput() {
        uppLibAdditionalWgtageRIGHT.setFilters(new InputFilter[]{new InputFilterMinMax("0", "10")});
        uppLibAdditionalWgtageLEFT.setFilters(new InputFilter[]{new InputFilterMinMax("0", "10")});
    }

}
